package com.xy.bizportdemo.database.base;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

/**
 * Created by XIAOYUAN on 2016/11/23.
 */
public abstract class DBHelper extends SQLiteOpenHelper {
    private static final String TAG = "DBHelper";
    private static final int MAX_TABLES = 10;

    protected final UriMatcher sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    protected final String[] sTableNames = new String[MAX_TABLES];
    private SQLiteDatabase mDatabase;

    public DBHelper(Context context, String name, CursorFactory factory,
                    int version) {
        super(context, name, factory, version);
        // for convenient array access
        sTableNames[0] = "NA";
    }

    public SQLiteDatabase getDatabase() {
        return this.mDatabase;
    }

    private void openDataBase() {
        try {
            this.mDatabase = getWritableDatabase();
            Log.i(TAG, "openDataBase: ");
        } catch (Exception e) {
            this.mDatabase = null;
        }
    }

    public void closeDataBase() {
        this.mDatabase.close();
    }

    abstract protected void createTable(SQLiteDatabase db);
    abstract protected void dropTable(SQLiteDatabase db);

    protected String getTableName(int index) {
        return sTableNames[index];
    }

    protected String getDefaultOrder() {
        return null;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTable(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        dropTable(db);
        createTable(db);
    }

    private void ensureDatabaseOpen() {
        if ((this.mDatabase == null) || (!this.mDatabase.isOpen())) {
            openDataBase();
        }
    }

    public Uri insert(Uri uri, ContentValues values) {
        ensureDatabaseOpen();
        if (values == null || values.size() <= 0) {
            return null;
        }

        long rowId = this.mDatabase.insert(getTableName(sMatcher.match(uri)), null, values);
        if (rowId < 0) {
            return null;
        }

        return ContentUris.withAppendedId(uri, rowId);
    }

    public int delete(Uri uri, String where, String[] whereArgs) {
        ensureDatabaseOpen();
        int count = this.mDatabase.delete(getTableName(sMatcher.match(uri)), where, whereArgs);
        return count;
    }

    public int update(Uri uri, ContentValues values, String userWhere, String[] whereArgs) {
        ensureDatabaseOpen();
        if (values == null || values.size() <= 0) {
            return 0;
        }

        int count = this.mDatabase.update(getTableName(sMatcher.match(uri)), values, userWhere, whereArgs);
        return count;
    }

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sort) {
        ensureDatabaseOpen();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        qb.setTables(getTableName(sMatcher.match(uri)));

        Cursor cursor = qb.query(this.mDatabase, projection, selection, selectionArgs, null, null, sort == null ? getDefaultOrder() : sort);
        return cursor;
    }
}
